<?php

namespace app\admin\controller\order;

use app\common\controller\Backend;

/**
 * 订单管理
 *
 * @icon fa fa-circle-o
 */
class Delivery extends Backend
{

    /**
     * Order模型对象
     * @var \app\admin\model\Order
     */
    protected $model = null;
    protected $noNeedLogin = [];
    protected $noNeedRight = ['ajaxconfirmorder','ajaxshippingorder'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Order');
        $this->view->assign("orderStatusList", $this->model->getOrderStatusList());
        $this->view->assign("payStatusList", $this->model->getPayStatusList());
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign("deletedList", $this->model->getDeletedList());
        $this->view->assign("shippingStatusList", $this->model->getShippingStatusList());
        $this->view->assign("orderPromTypeList", $this->model->getOrderPromTypeList());
        $this->view->assign("goodsNumList", $this->model->getGoodsNumList());
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        if ($this->request->isAjax())
        {
            //公共条件
            //shipping_status = 0 , pay_status = 1, order_status = 1
            //, type = 0 或者 type != 0 && first_user_id > 0


            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->where(function ($query){
                    $query->where(['shipping_status' => 0, 'pay_status' =>1,'order_status' => 1]);
                })
                ->where(function ($query){
                    $query->where(['type' => 0])->whereOr(['type' => ['<>',0], 'first_user_id' => ['>',0]]);
                })
                ->order($sort, $order)
                ->count();



            $list = $this->model
                ->where($where)
                ->where(function ($query){
                    $query->where(['shipping_status' => 0, 'pay_status' =>1,'order_status' => 1]);
                })
                ->where(function ($query){
                    $query->where(['type' => 0]);
                    $query->whereOr(['first_user_id' => ['>',0]]);
                })
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

//            return json($this->model->getLastSql());

            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 查看
     */
    public function detail($ids = NULL)
    {
        $row = $this->model->get($ids);

        if (!$row)
            $this->error(__('No Results were found'));


        $data = $row->toArray();

        $hiddenarr = [
            'pay_time','order_prom_type','user_id','goods_id','first_user_id,deleted'
        ];
        foreach ($data as $k => $v){
            if(in_array($k,$hiddenarr)){
                unset($data[$k]);
            }
        }

        $address = \think\Db::name('address')->where(['id' => $row['address_id']])->find();

        $this->view->assign('shippingList',$this->model->getshippingList());
        $this->view->assign("row",$data);
        $this->view->assign('address',$address);
        return $this->view->fetch();
    }




}
